using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;
using Microsoft.Xna.Framework.Content;

namespace MCGameEngine.Physic_Layer
{
    public class PhysicObject : BasicObject
    {
        public Vector3 Velocity;
        public Vector3 Acceleration;
        public Vector3 SpinningVelocity;
        public Vector3 SpinningAcceleration;

        public bool Collidable;

        public PhysicObject()
        {
            Velocity = Vector3.Zero;
            Acceleration = Vector3.Zero;
            SpinningVelocity = Vector3.Zero;
            SpinningAcceleration = Vector3.Zero;
        }

        public void AddSpinningForce(Vector3 newton)
        {
            SpinningAcceleration = SpinningAcceleration + (newton / Mass);
        }

        public void AddSpinningWork(Vector3 joule)
        {
            SpinningVelocity = SpinningVelocity + (joule / Mass);
        }

        public void AddForce(Vector3 newton)
        {
            Acceleration = Acceleration + (newton / Mass);
        }

        public void AddWork(Vector3 joule)
        {
            Velocity = Velocity + (joule / Mass);
        }

        public override void Update(GameTime gameTime)
        {
            float elapsed = (float)gameTime.ElapsedGameTime.TotalSeconds;

            Velocity = Velocity + Acceleration * elapsed;
            SpinningVelocity = SpinningVelocity + SpinningAcceleration * elapsed;
            this.Rotate(SpinningVelocity * elapsed);
            this.Move(Velocity * elapsed + Position);

            base.Update(gameTime);
        }

        public override void Draw(Camera camera)
        {
            if (Model == null)
            {
                return;
            }

            Matrix[] transforms = new Matrix[Model.Bones.Count];
            Model.CopyAbsoluteBoneTransformsTo(transforms);

            foreach (ModelMesh mesh in Model.Meshes)
            {
                foreach (BasicEffect effect in mesh.Effects)
                {
                    effect.EnableDefaultLighting();
                    effect.PreferPerPixelLighting = true;
                    effect.World = transforms[mesh.ParentBone.Index] * World;
                    effect.View = camera.View;
                    effect.Projection = camera.Projection;
                }

                mesh.Draw();
            }
        }
    }
}